package com.tangye.massageshop.service.impl;

import com.tangye.massageshop.constants.SysConstant;
import com.tangye.massageshop.enums.RoleEnum;
import com.tangye.massageshop.enums.ShopStatusEnum;
import com.tangye.massageshop.exception.ShopException;
import com.tangye.massageshop.repository.ShopRepository;
import com.tangye.massageshop.service.ShopService;
import com.tangye.massageshop.service.UserService;
import com.tangye.massageshop.entity.ShopEntity;
import com.tangye.massageshop.entity.UserEntity;
import com.tangye.massageshop.mapper.ShopMapper;
import com.tangye.massageshop.util.MD5Utils;
import com.tangye.massageshop.util.ThreadLocalUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Log4j2
@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopRepository shopRepository;
    @Resource
    private ShopMapper shopMapper;

    @Resource
    private UserService userService;

    @Override
    public List<ShopEntity> queryShopListPage(ShopEntity shop) {
        return shopRepository.selectShopPage(shop);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserEntity add(ShopEntity shop) {
        ShopEntity shopEntity = shopRepository.selectByName(shop.getShopName());
        if (Objects.nonNull(shopEntity)) {
            throw new ShopException("店铺已存在");
        }
        shop.setShopStatus(ShopStatusEnum.NORMAL.getCode());
        shopMapper.insert(shop);

        UserEntity userSession = ThreadLocalUtil.get();
        UserEntity userEntity = new UserEntity();
        userEntity.setCompanyId(shop.getCompanyId());
        userEntity.setCompanyName(userSession.getCompanyName());
        userEntity.setUserName(RandomStringUtils.randomAlphabetic(6));
        userEntity.setPassword(MD5Utils.encryptByMd5(SysConstant.INIT_PASSWORD));
        userEntity.setRole(RoleEnum.SHOP.getCode());
        userService.add(userEntity);
        return userEntity;
    }

    @Override
    public void updateShop(ShopEntity shop) {
        if (Objects.nonNull(shop.getShopId())) {
            ShopEntity shopEntityDB = shopMapper.selectById(shop.getShopId());
            if (Objects.isNull(shopEntityDB)) {
                log.error("店铺不存在");
                return;
            }
            ShopEntity shopEntity = new ShopEntity();
            shopEntity.setShopId(shop.getShopId());
            shopEntity.setShopName(shop.getShopName());
            if (StringUtils.isNotBlank(shop.getShopContact())) {
                shopEntity.setShopContact(shop.getShopContact());
            }
            if (StringUtils.isNotBlank(shop.getShopAddress())) {
                shopEntity.setShopAddress(shop.getShopAddress());
            }
            if (StringUtils.isNotBlank(shop.getShopPhone())) {
                shopEntity.setShopPhone(shop.getShopPhone());
            }

            shopMapper.updateById(shopEntity);
        }
    }
}
